(function(){
    angular.module('app').controller("agQualificationCtrl",function($scope,$stateParams,proxy){
        var id = $stateParams.id;
        $scope.isEditInfo = false;
        $scope.isEditPwd = false;
        $scope.quaList = [];
        $scope.obj={
            isEditInfo:true,
            flag:true,
            tempArr:[],
            permission:PERMISSION,
        };

        $scope.addQua = function(){
            $scope.quaList.push({
                name:'',
                url:'',
            })
            upImg($scope,proxy,'upOther',$scope.quaList);

        };
        $scope.deleteQua = function(index) {
            $scope.quaList.splice(index,1);
        };
        // getList();
        // function getList(){
        //     proxy.proxy({
        //         url: STATIC_URL.domian+'/dsp/advertiser/qualification/list',
        //         data:{
        //             "filter": {
        //                 "orgID":id
        //             },
        //         },
        //         success: function(res){
        //             $scope.list = res.data;
        //             upImg($scope,proxy,'upload',$scope.list);
        //         }
        //     });
        // }
        $scope.upLoad = function(flag){
            $scope.quaList = [];
            $scope.obj.flag = !$scope.obj.flag;
            $scope.obj.isEditInfo = !$scope.obj.isEditInfo;
        };
        // $scope.checkUrl = function(url) {
        //     var reg = /^https?:\/\//;
        //     if(!reg.test(url)) return layer.msg("公司网址格式不正确！格式为http://xxxxx");
        //     window.open(url)
        // }
        //保存上传
        $scope.saveQualification = function(){
            var tempArr = $scope.list,
                quaList = $scope.quaList,
                reg = /^https?:\/\//,
                value;
            for(var i = 0;i<tempArr.length;i++) {
                value = tempArr[i];
                if(value.url == '' && value.isNecessary){
                    if(value.isString) {
                        layer.msg(value.name+'未输入')
                    } else {
                        layer.msg(value.name+'未上传')
                    }
                    return;
                }
                if(value.name == '公司网址' && value.url != "") {
                    if(!reg.test(value.url)) return layer.msg("公司网址格式不正确！")
                }
                // 设置广告主id
                value.id = id;
            }
            for(var i = 0;i < quaList.length;i++) {
                value = quaList[i];
                if(!value.name) {
                    return layer.msg("请输入资质名称！")
                }
                if(!value.url) {
                    return layer.msg("请上传"+value.name+"的图片！")
                }
                tempArr.push({
                    id: id,
                    name: value.name,
                    md5: value.md5,
                    url: value.url
                })
            }
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/qualification/adds',
                data:{
                    data: tempArr
                },
                success: function(res){
                    $scope.obj.isEditInfo = !$scope.obj.isEditInfo;
                    getList();
                    layer.msg('保存成功');
                }
            })
        }

        // 显示相片弹框
        $scope.showPhoto = function(url){
            if(url) {
                layer.photos({
                    photos:{
                        "title": "", //相册标题
                        "id": 1, //相册id
                        "start": 0, //初始显示的图片序号，默认0
                        "data": [   //相册包含的图片，数组格式
                            {
                                "alt": "",
                                "pid": 2, //图片id
                                "src": url, //原图地址
                                "thumb": "" //缩略图地址
                            }
                        ]
                    },
                    anim: 5
                })
            }
        }
    })
})();


//上传按钮初始化
function upImg(scope,proxy,lem,list){
    if(list.length !=0){
        list.forEach(function(v,i){
            fileUpload({
                'btnId':lem+i,
                'BeforeUpload': function (up, file,cb) {
                    var reader = new FileReader();
                    reader.onload = function() {
                        v.md5 = hex_md5(reader.result)
                        cb && cb();
                    }
                    reader.readAsBinaryString(file.nFile)
                },
                'funcUrl': '2',
                'FileUploaded':function(info, index){
                    scope.$apply(function () {
                        list[index].url = info.url;
                        // list[index].md5 = info.md5;
                        // proxy.proxy({
                        //     url: STATIC_URL.domian+'/dsp/advertiser/qualification/add',
                        //     data:{
                        //         //"orgID":"10",
                        //         "name":list[index].name,
                        //         "url":list[index].url,
                        //         "md5":list[index].md5,
                        //     },
                        //     success: function(res){
                        //         layer.msg("图片保存成功!")
                        //     }
                        // })

                    })
                },
                index: i
            })
        })
    }
};